# coding: utf-8

import json
import time
import urllib2
import requests

def current_time():
    return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))

def send_get(url, params, headers=None):
    url += '?'
    for k,v in params.items():
        url += "%s=%s&" % (k,urllib2.quote(str(v), safe=':=/\-'))
    url = url.strip("&")
    # print url
    # print headers
    # r = urllib2.urlopen(url)
    ret = requests.get(url, headers=headers)
    ret = ret.content
    # print ret
    # print '-' * 20
    return ret

def send_post(url, params, headers=None, jsondump=True):
    header = {'Connection' : 'close'}
    if headers is not None:
        for k,v in headers.items():
            header[k] = v
    r = ""
    if jsondump:
        header["Content-Type"] = "application/json; charset=UTF-8"
        r = requests.post(url, data=json.dumps(params), headers=header)
    else:
        r = requests.post(url, data=params, headers=header)
    ret = r.content
    # print '-' * 20
    # print url
    # print params
    # print '-' * 20
    # print '>>', ret
    return ret